package blf.librarysystem.Service;

import blf.librarysystem.Dao.BookMapper;
import blf.librarysystem.Dao.ReaderMapper;
import blf.librarysystem.Pojo.Book;
import blf.librarysystem.Pojo.Reader;
import blf.librarysystem.Utils.Result;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookServiceIMP implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private ReaderMapper readerMapper;

    @Override
    public Result<Book> findBookList(int start, int limit) {
        Result<Book> result = new Result<>();
        result.setCode(0);
        result.setMsg("查询成功");
        result.setCount(bookMapper.getTotalCount());
        result.setData(bookMapper.listAll(start, limit));

        List<Book> books = bookMapper.listAll(start, limit);
        for (Book book : books) {
            String borrowname = null;
            Reader reader = readerMapper.findByPrimaryKey(book.getReaderId());
            if (reader != null) {
                borrowname = reader.getReaderName();
            }

            if (book.getBorrowStatus().equals("0")) {
                book.setStatusIntroduce("暂无借阅");
                book.setAction("借阅");
            }
            if (book.getBorrowStatus().equals("1")) {
                book.setStatusIntroduce("已借阅");
                book.setAction("归还");
            }
            if (book.getBorrowStatus().equals("2")) {
                book.setStatusIntroduce("已归还");
                book.setAction("借阅");
            }

            book.setBorrowName(borrowname);
        }

        result.setData(books);
        return result;
    }

    @Override
    public Result<Book> borrowBook(int bookId, int borrowId) {
        Result<Book> result = new Result<>();
        result.setCode(0);
        result.setMsg("借阅成功");

        Book book = bookMapper.selectByPrimaryKey(bookId);
        book.setReaderId(borrowId);
        book.setBorrowTime(new Date());
        bookMapper.updateByPrimaryKeySelective(book);
        bookMapper.borrowBook(borrowId, bookId);
        return result;
    }

    @Override
    public Result<Book> returnBook(int id) {

        Result<Book> result = new Result<>();
        result.setCode(0);
        result.setMsg("归还成功");

        bookMapper.returnBook(id);
        return result;
    }

    @Override
    public Result<Book> listByKeys(String readerName, String bookName, String borrowStatus, int start, int limit) {
        Result<Book> result = new Result<>();
        result.setCode(0);
        result.setMsg("查询成功");
        result.setCount(bookMapper.getCountByKeys(bookName, borrowStatus));
        result.setData(bookMapper.listByKeys(bookName, borrowStatus, start, limit));

        List<Book> books = bookMapper.listByKeys(bookName, borrowStatus, start, limit);
        List<Book> middle = new ArrayList<>();
        for (Book book : books) {
            Reader reader = readerMapper.findByPrimaryKey(book.getReaderId());
            String borrowname = reader.getReaderName();
            if (borrowname.contains(readerName)) {
                if (book.getBorrowStatus().equals("0")) {
                    book.setStatusIntroduce("暂无借阅");
                    book.setAction("借阅");
                }
                if (book.getBorrowStatus().equals("1")) {
                    book.setStatusIntroduce("已借阅");
                    book.setAction("归还");
                }
                if (book.getBorrowStatus().equals("2"))
                    book.setStatusIntroduce("已归还");
                book.setAction("借阅");
            }
            book.setBorrowName(borrowname);
            middle.add(book);
        }
        result.setData(middle);
        return result;
    }
}

